<?php
class ome_sync_product{
    
    function add_stock_sync(){
        $shop_info = kernel::database()->select("SELECT shop_id,node_type,node_id FROM sdb_ome_shop WHERE node_id IS NOT NULL");
        if($shop_info){
            foreach($shop_info as $v){
            	if (!$v['node_id']) continue;
            	
            	$shop_id = $v['shop_id'];
            	$node_type = $v['node_type'];
            	$queue_title = "sync_stock_".$shop_id;
            	
            	//判断该店铺是否回写库存
            	$shop_stock_config = &app::get('ome')->getConf('shop_stock_config_'.$shop_id);
            	
            	if ($shop_stock_config == 'false'){
            		continue;
            	}
            	
              
                if(!kernel::database()->selectrow("SELECT queue_id FROM sdb_base_queue WHERE worker='ome_sync_product.sync_stock' AND queue_title='".$queue_title."'")){
                    $last_store_sync_end = app::get('ome')->getConf('store_sync_end'.$shop_id);
                    $store_sync_from = $last_store_sync_end?$last_store_sync_end:0;
                    $store_sync_end = time();
                    
                    app::get('ome')->setConf('store_sync_from'.$shop_id,$store_sync_from);
                    app::get('ome')->setConf('store_sync_end'.$shop_id,$store_sync_end);
                    
                    $params = array(
                            'store_sync_from'=>$store_sync_from,
                            'store_sync_end'=>$store_sync_end,
                            'shop_id' => $shop_id,
                            'node_type' => $node_type,
                        );
                       
                    $data = array(
                        'queue_title'=>$queue_title,
                        'start_time'=>time(),
                        'params'=>$params,
                        'cursor_id' => 0,
                        'worker'=>'ome_sync_product.sync_stock',
                    );
                   
                    $queue_id = app::get('base')->model('queue')->insert($data);
                    kernel::log("stock_queue:".$queue_id);
                    app::get('base')->model('queue')->runtask($queue_id);
                    
                   // $log = &app::get('ome')->model('api_log');
                   // $log->write_log($log->gen_id(), '库存增加同步,队列ID：'. $queue_id . ' 店铺ID：'. $shop_id, 'ome_sync_product', 'add_stock_sync', '', '', 'response', 'success', var_export($params, true) . '<BR>'. var_export($data, true));
                }
            }
            return true;
        }else{
            return false;
        }
    }
    
    function sync_stock(&$cursor_id,$params){        
  	
        if (!is_array($params)){
            $params = unserialize($params);
        }
        $limit = 50;
        $shop_id = $params['shop_id'];
        $node_type = $params['node_type'];
        $store_sync_from = $params['store_sync_from'];
        $store_sync_end = $params['store_sync_end'];
        $offset = $cursor_id;
 
        //获取回写库存
        if ($stock_service = kernel::service('service.stock')){
            if(method_exists($stock_service,'calculate_stock')){
                $stocks = $stock_service->calculate_stock($shop_id, $store_sync_from, $store_sync_end, $offset, $limit);
            }
        }
        kernel::log("stocks:".$shop_id.json_encode($stocks));
        if ($stocks){
        	foreach(kernel::servicelist('service.stock') as $object=>$instance){
                if(method_exists($instance,'update_stock')){
                    $instance->update_stock($stocks,$shop_id,$node_type);
                }
            }
            $offset = $offset + $limit;
            $cursor_id = $offset;
           // app::get('ome')->model('shop')->update(array('last_store_sync_time'=>$store_sync_end),array('shop_id'=>$shop_id));
            return true;
        }else{
            return false;
        }
    }
}